import { DictTag } from "@/components/DictTag";
import { getDictObj } from "@/utils/dict";
import moment from "moment";
import React from "react";

export const useRender = {
	/**
	 * 渲染日期
	 * @param text 日期
	 * @param format 格式化
	 * @returns 格式化后日期
	 */
	renderDate: (text: string, format?: string) => {
		if (!text) return "";

		if (!format) return moment(text).format("YYYY-MM-DD HH:mm:ss");
		else return moment(text).format(format);
	},
	/**
	 * 渲染字典
	 * @param text 字典值
	 * @param dictType 字典类型
	 * @returns 字典标签
	 */
	renderDict: (text: string, dictType: string): JSX.Element | null => {
		if (dictType)
			return React.createElement(DictTag, {
				type: dictType,
				value: text
			});
		return null;
	},
	/**
	 * 渲染字典
	 * @param text 字典值
	 * @param dictType 字典类型
	 * @returns 字典标签
	 */
	renderDictText: (text: string, dictType: string) => {
		let dict = null;
		if (dictType) dict = getDictObj(dictType, text);
		return dict ? dict.label : dict;
	}
};
